﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jaysJourney_v01.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using jaysJourney_v01.Utils;
using Microsoft.Xna.Framework.Input;

namespace jaysJourney_v01.States
{
    class DeathState : State
    {
        /**
         * MEMBER VARIABLES
         * */

        // Common
        private SpriteBatch mainSpriteBatch;
        private InputGroup menuButtonGroup;

        private Texture2D userdataBG;
        private Texture2D interfaceBG;

        private bool loading;
        private Texture2D loadingscreen;

        // Buttons
        private Button restart;
        private Button loadWorld;
        private Button mainMenu;

        /**
         * METHODS
         * */

        public DeathState(String name, StateManager stateManager) : base(name, stateManager) { }

        public override void initialize()
        {
            mainSpriteBatch = parentSpriteBatch;

            this.interfaceBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\main_menu_v2");
            this.userdataBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\userdata_bg");
            this.loadingscreen = this.getGame().Content.Load<Texture2D>(@"Textures\Preloader\loadingscreen");


            // Buttons
            this.restart = new Button(this, "RESTART", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 350, Color.White, false, true);
            this.loadWorld = new Button(this, "LOAD WORLD", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 450, Color.White, false, true);
            this.mainMenu = new Button(this, "MAIN MENU", @"Textures\Interface\back_btn", @"Textures\Interface\back_btn_ac", 20, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);

            this.menuButtonGroup = new InputGroup(this, this.mainSpriteBatch);
            this.menuButtonGroup.addButton(restart);
            this.menuButtonGroup.addButton(loadWorld);
            this.menuButtonGroup.addButton(mainMenu);
        }

        public override void update(GameTime gameTime)
        {

            // update buttons
            Button click = this.menuButtonGroup.update(gameTime);

            if (loading)
            {
                Console.WriteLine("gimme");
                loading = false;
                PlayState playState = ((PlayState)this.getStateManager().getState("playState"));
                playState.resetLevel();
                //playState.getLvlData().setNumStarts(playState.getLvlData().getNumStarts() + 1);
                //WebSaver.worldUpdate(playState.getLvlData().getLevel(), playState.getLvlData().getNumStarts(), playState.getLvlData().getNumRates(), playState.getLvlData().getCurRating(), playState.getLvlData().getBestTime(), playState.getLvlData().getRecordHolder(), playState.getLvlData().getAvgBestTime());
                this.getStateManager().setActiveState("playState");
            }
            // handle button clicks
            if (click != null && click.getName() == "RESTART"){
                loading = true;
            }
            else if (click != null && click.getName() == "LOAD WORLD")
            {
                ((LoadWorldState)this.getStateManager().getState("loadWorldState")).loadworldoverview = true ;
                this.getStateManager().setActiveState("loadWorldState");
            }
            else if (click != null && click.getName() == "MAIN MENU") this.getStateManager().setActiveState("menuState");
        }

        public override void draw(GameTime gameTime)
        {
            // draw bg
            this.mainSpriteBatch.Draw(this.getGame().menuBg, new Rectangle(0, 0, this.getGame().GraphicsDevice.Viewport.Width, this.getGame().GraphicsDevice.Viewport.Height), new Rectangle(0, 0, this.getGame().menuBg.Width, this.getGame().menuBg.Height), Color.White);

            // draw interface bg
            this.mainSpriteBatch.Draw(this.userdataBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) + 170, 350, this.userdataBG.Width, this.userdataBG.Height), new Rectangle(0, 0, this.userdataBG.Width, this.userdataBG.Height), Color.White);
            this.mainSpriteBatch.Draw(this.interfaceBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, 0, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // draw buttons
            this.menuButtonGroup.draw(gameTime);

            // draw text
            this.mainSpriteBatch.DrawString(this.getGame().textFont, "Y O U   A R E   D E A D ", new Vector2(this.getGame().GraphicsDevice.Viewport.Width / 2 + 215, 360), Color.White);
            if (loading) this.mainSpriteBatch.Draw(this.loadingscreen, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.loadingscreen.Width / 2, (this.getGame().GraphicsDevice.Viewport.Height / 2) - this.loadingscreen.Height / 2, this.loadingscreen.Width, this.loadingscreen.Height), new Rectangle(0, 0, this.loadingscreen.Width, this.loadingscreen.Height), Color.White);

        }

        public override void onEnterState()
        {
            loading = true;
            this.restart.setNewPosition(350);
            this.loadWorld.setNewPosition(450);
            this.mainMenu.setNewPosition(20, this.getGame().GraphicsDevice.Viewport.Height - 74);
        }
    }
}
